package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.RoleDTO;
import com.mrdeer.common.entity.sys.Role;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.RoleMapper;
import com.mrdeer.web.service.sys.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements SysRoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public Role add(Role role) {
        if (roleMapper.insert(role) > 0) {
            return roleMapper.selectById(role.getId());
        }
        throw new BusinessException("The role add failed!");
    }

    @Override
    public boolean delete(String id) {
        Role role = getById(id);
        if (null == role) {
            throw new BusinessException("The role does not exist");
        }
        return roleMapper.deleteById(id) > 0;
    }

    @Override
    public Role update(Role role) {
        role.setUpdateTime(null);
        if (roleMapper.updateById(role) > 0) {
            Role r = roleMapper.selectById(role.getId());
            return r;
        }
        throw new BusinessException("The role update failed");
    }

    @Override
    public Role getById(String id) {
        return roleMapper.selectById(id);
    }

    @Override
    public Page<Role> list(RoleDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return roleMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<Role> fuzzyQuery(RoleDTO dto) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.<Role>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getName())) {
                queryWrapper.like(Role::getName, dto.getName());
            }
            if (dto.getDefaultRole() != null) {
                queryWrapper.eq(Role::getDefaultRole, dto.getDefaultRole());
            }
            if (StrUtil.isNotBlank(dto.getKey())) {
                queryWrapper.like(Role::getName, dto.getKey()).or().like(Role::getDescription, dto.getKey());
            }
        }
        return queryWrapper;
    }

    @Override
    public List<Role> findByDefaultRole(Boolean defaultRole) {
        RoleDTO dto = new RoleDTO();
        dto.setDefaultRole(defaultRole);
        List<Role> roles = roleMapper.selectList(fuzzyQuery(dto));
        return roles;
    }

    @Override
    public List<Role> findByCondition(RoleDTO dto) {
        return roleMapper.selectList(fuzzyQuery(dto));
    }
}
